//
//  NoteListViewController.m
//  UpNote
//
//  Created by Justsoso on 16/4/2.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "NoteListViewController.h"

#import "AddViewController.h"

#import "NoteModel.h"

#import "NetManager.h"

@interface NoteListViewController ()<addviewControllerDelegate>

@property (nonatomic,strong) NSMutableArray *noteList;

@property (nonatomic,strong) NSIndexPath *curSel;



@end

@implementation NoteListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self searchNote];
}

- (void)searchNote
{
    [NetManager requestSearchNoteWith:^(NSArray *arr) {
        self.noteList = [NSMutableArray array];
        if (arr != nil)
        {
            for (NSDictionary *d in arr)
            {
                NoteModel *n = [NoteModel noteWithDict:d];
                [self.noteList addObject:n];
                [self.tableView reloadData];
            }
        }
    }];
}

- (IBAction)editBtnPressed:(id)sender
{
    self.tableView.editing = !self.tableView.editing;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    AddViewController *addVC = segue.destinationViewController;
    addVC.delegate = self;
    if([segue.identifier isEqualToString:@"goEdit"])
    {
        NoteModel *m = self.noteList[self.curSel.row];
        addVC.note = m;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.noteList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    // Configure the cell...
    
    NoteModel *n = self.noteList[indexPath.row];
    
    cell.textLabel.text = n.Content;
    cell.detailTextLabel.text = n.CDate;
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.noteList[indexPath.row];
    [NetManager requestRemoveNoteWithNoteId:dict[@"ID"] successBlock:^{
        [self.noteList removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.curSel = indexPath;
    return indexPath;
}

- (void)addNoteContent:(NSString *)content
{
    NoteModel *n = [[NoteModel alloc] init];
    n.Content = content;
    NSDate *now = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterShortStyle;
    df.timeStyle = NSDateFormatterNoStyle;
    n.CDate = [df stringFromDate:now];
    
    
    [NetManager requestAddNoteWithContent:n.Content andTime:n.CDate successBlock:^{
        [self searchNote];
        [self.noteList insertObject:n atIndex:0];
    }];
}

- (void)refresh
{
    [self.tableView reloadData];
}

@end
